/**
 * @file    led.c
 * @author ait_zhengchunyu (ait_zhengchunyu@163.com)
 * @brief  led 应用层相关处理函数和逻辑
 * @version 0.1
 * @date 2022-02-07
 * 
 * @copyright Copyright (c) 2022
 * 
 */
#include "led.h"
#include "../dbg/dbg.h"

static struct led_dev_t led_device;
//#define LED_TEST 
#ifdef LED_TEST
void led_delay(u32 cnt)
{
	u32 t_cnt;

	for (t_cnt = 0; t_cnt < cnt; t_cnt++)
	{
		__nop();
	}
}
void led_test(void)
{

  while (1)
  {
   // led_device.obj[0].toggle();
   // led_device.obj[1].toggle();
     led_device.obj[2].toggle();
    led_delay(0xffff);

  }
}
#endif 


/**
 * @brief 设置led 状态，根据状态 和闪烁时间 设置led状态
 * 
 * @param dev   led_dev_t
 * @param num   led编号
 * @param state 
 * @return int 
 */
static int led_set_state(struct led_dev_t *dev , uint8_t num)
{
   
    if(dev == NULL)
    return -1;
    if(num>=LED_MAX_NUM)
    return -2;
		
    switch(dev->obj[num].state)//根据状态 调用相关函数
    {
        case LED_STATE_ON:
            dev->obj[num].on();
        break;
        case LED_STATE_OFF:
            dev->obj[num].off();
        break;
        case LED_STATE_BLK:
            if(dev->obj[num].blk_cnt>0)
            {
                dev->obj[num].blk_cnt--; 
            }
            else
						{
                dev->obj[num].toggle();
                dev->obj[num].blk_cnt = dev->obj[num].blk_setval;//赋值，准备下一次触发
            }
        break;
    }
		return 0;
}
uint32_t ledSync=0;
static void led_sync_state(struct led_dev_t *dev)
{
	uint8_t i;
	for(i=0;i<LED_MAX_NUM;i++)
	{
		//if((dev->obj[i].state)!= LED_STATE_OFF)
		//{
				dev->obj[i].off();
		//}
      dev->obj[i].blk_cnt = dev->obj[i].blk_setval;//赋值，准备下一次触发
	}

}
/*
 * @brief 每隔500ms调用一次
 */
static int led_timer_functions(struct led_dev_t *dev)
{
    if(dev==NULL)
    {
        return -1;
    }

		ledSync++;
    if(ledSync>=20)//每隔10s 调用一次同步函数
    {
			ledSync = 0;
			led_sync_state(dev);
		}
		else{
			led_set_state(dev,LED_NUM_RUNNING);
			led_set_state(dev,LED_NUM_ALARMED);
			led_set_state(dev,LED_NUM_WARNING);
		}
	return 0;
}
struct led_dev_t *led_device_ponter(void)
{   
    return &led_device;
}
/**
 * @brief 设置led 的闪烁值
 * 
 * @param dev  设备
 * @param num 设置的编号
 * @param val  设置值 以500ms为单位
 * @return int 设置成功返回0
 */
int led_set_blk(struct led_dev_t *dev , uint8_t num,uint16_t val)
{
    if(dev == NULL)
    return -1;
    if(num>=LED_MAX_NUM)
    return -2;
    dev->obj[num].blk_setval = val;
    return 0;
}
/**
 * @brief 修改led 执行状态
 * 
 * @param dev 设备实例
 * @param num 设置的编号
 * @param state_val 设置的状态 
 * @return int 
 */
int modify_state(struct led_dev_t *dev , uint8_t num,enum LED_STATE state_val)
{
    if(dev == NULL)
    return -1;
    if(num>=LED_MAX_NUM)
    return -2;
    dev->obj[num].state = state_val;
    return 0;
}
/**
 * @brief 返回对应编号led 的状态
 * 
 * @param dev 设备实例
 * @param num  编号
 * @return enum LED_STATE 状态值
 */
enum LED_STATE led_get_state(struct led_dev_t *dev , uint8_t num)
{
    return dev->obj[num].state;
}
static void led_object_init(void)
{
    led_device.obj[0].num    = 0               ;//编号
    led_device.obj[0].blk_val= 2              ;//闪烁设定值，闪烁时间规零后赋值
    led_device.obj[0].blk_cnt= 2             ;//闪烁时间
	  led_device.obj[0].blk_setval  = 2;
    led_device.obj[0].state  =  LED_STATE_BLK  ;
    led_device.obj[0].on     =   led0_on             ;//打开
    led_device.obj[0].off    =   led0_off              ;//关闭
    led_device.obj[0].toggle =   led0_toggle              ;//切换
    led_device.obj[0].pin_state = led0_pin_state;

    led_device.obj[1].num    = 1               ;//编号
    led_device.obj[1].blk_val= 2               ;//闪烁设定值，闪烁时间规零后赋值
    led_device.obj[1].blk_cnt= 2               ;//闪烁时间
	led_device.obj[1].blk_setval  = 2;
    led_device.obj[1].state  =  LED_STATE_BLK  ;
    led_device.obj[1].on     =   led1_on                  ;//打开
    led_device.obj[1].off    =   led1_off                 ;//关闭
    led_device.obj[1].toggle =   led1_toggle              ;//切换
    led_device.obj[1].pin_state = led1_pin_state;

    led_device.obj[2].num    = 1              ;//编号
    led_device.obj[2].blk_val= 2               ;//闪烁设定值，闪烁时间规零后赋值
    led_device.obj[2].blk_cnt= 2               ;//闪烁时间
	  led_device.obj[2].blk_setval  = 2;
    led_device.obj[2].state  =  LED_STATE_BLK   ;
    led_device.obj[2].on     =   led2_on                  ;//打开
    led_device.obj[2].off    =   led2_off                 ;//关闭
    led_device.obj[2].toggle =   led2_toggle              ;//切换
    led_device.obj[2].pin_state = led2_pin_state;

}
void led_init(struct led_dev_t **dev)
{
    led_gpio_init();
    led_object_init();
    led_device.led_count            = LED_MAX_NUM;
    led_device.set_state            = led_set_state;
    led_device.set_led_blk          = led_set_blk;
    led_device.modify_state         = modify_state;
    led_device.get_state            = led_get_state;
    led_device.get_device_ponter    = led_device_ponter;
    led_device.timer_callback       = led_timer_functions;
		*dev = &led_device;
    #ifdef LED_TEST
    led_test();
    #endif 
} 




